//
//  XJForgetPasswdView.m
//  XinJiangMall
//
//  Created by Tzyang on 2018/4/8.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJForgetPasswdView.h"
#import "XJCodeView.h"


@interface XJForgetPasswdView ()<UITextFieldDelegate,XJCodeDelegate>

@property (nonatomic ,strong) XJCodeView * xjCodeView;

@property (nonatomic ,strong) UIButton * xjfp_loginBtn;

@end

@implementation XJForgetPasswdView

- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = WHITE_COLOR;
        [self setupSubviews];
    }
    return self;
}

- (ZYTextField *)fpPhoneTextField{
    
    if (!_fpPhoneTextField) {
        ZYTextField * fpPhoneTextField = [[ZYTextField alloc]initWithFrame:CGRectMake(ADAPTWidth(23), ADAPTHeight(30), KWidth - ADAPTWidth(23)*2, ADAPTHeight(50)) placeholder:@"请输入手机号" keyboardType:UIKeyboardTypePhonePad leftViewImageName:@"login_phone" textEntry:NO];
        self.fpPhoneTextField = fpPhoneTextField;
        [self.fpPhoneTextField addTarget:self action:@selector(regist_phoneTextDidChange:) forControlEvents:(UIControlEventEditingChanged)];
        
        [self addSubview:fpPhoneTextField];
    }
    return _fpPhoneTextField;
}

- (ZYTextField *)fpCodeTextField{
    
    if (!_fpCodeTextField) {
        ZYTextField * fpCodeTextField = [[ZYTextField alloc]initWithFrame:CGRectMake(ADAPTWidth(22), CGRectGetMaxY(self.fpPhoneTextField.frame)+0.5, KWidth - ADAPTWidth(22)*2, ADAPTHeight(50)) placeholder:@"请输入验证码" keyboardType:UIKeyboardTypeNumberPad leftViewImageName:@"login_verify" textEntry:NO];
        self.fpCodeTextField = fpCodeTextField;
        [self addSubview:fpCodeTextField];
    }
    return _fpCodeTextField;
}

- (ZYTextField *)fpPasswordTextField{
    
    if (!_fpPasswordTextField) {
        ZYTextField * fpPasswordTextField = [[ZYTextField alloc]initWithFrame:CGRectMake(ADAPTWidth(22), CGRectGetMaxY(self.fpCodeTextField.frame)+0.5, KWidth - ADAPTWidth(22)*2, ADAPTHeight(50)) placeholder:@"请输入新密码6-20位" keyboardType:UIKeyboardTypeDefault leftViewImageName:@"login_password" textEntry:NO];
        self.fpPasswordTextField = fpPasswordTextField;
        [self addSubview:fpPasswordTextField];
    }
    return _fpPasswordTextField;
    
}

- (ZYTextField *)fpRepasswordTextField{
    
    if (!_fpRepasswordTextField) {
        ZYTextField * fpRepasswordTextField = [[ZYTextField alloc]initWithFrame:CGRectMake(ADAPTWidth(23), CGRectGetMaxY(self.fpPasswordTextField.frame)+1, KWidth - ADAPTWidth(23)*2, ADAPTHeight(50)) placeholder:@"请确认新密码6-20位" keyboardType:UIKeyboardTypeDefault leftViewImageName:@"login_password" textEntry:NO];
        self.fpRepasswordTextField = fpRepasswordTextField;
        [self addSubview:fpRepasswordTextField];
    }
    return _fpRepasswordTextField;
}

- (XJCodeView *)xjCodeView{
    
    if (!_xjCodeView) {
        XJCodeView * xjCodeView = [[XJCodeView alloc]initWithTime:60 frame:CGRectMake(0, 0, 80, 30)];
        xjCodeView.codeBtn.userInteractionEnabled = NO;
        xjCodeView.codeBtn.backgroundColor = MAINS_COLOR;

        self.xjCodeView = xjCodeView;
    }
    return _xjCodeView;
    
}

- (UIButton *)xjfp_loginBtn{
    
    if (!_xjfp_loginBtn) {
        UIButton * xjfp_loginBtn = [UIButton new];
        self.xjfp_loginBtn = xjfp_loginBtn;
        [self addSubview:xjfp_loginBtn];
    }
    return  _xjfp_loginBtn;
}

- (void)setupSubviews{
    //手机号
    self.fpPhoneTextField.delegate = self;
    //验证码
    self.xjCodeView.delegate = self;
    self.fpCodeTextField.rightView = self.xjCodeView;
    self.fpCodeTextField.rightViewMode = UITextFieldViewModeAlways;
    //密码
    self.fpPasswordTextField.delegate = self;
    //确认密码
    self.fpRepasswordTextField.delegate = self;
    //登录
    self.xjfp_loginBtn.backgroundColor = MAIN_COLOR;
    [self.xjfp_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [self.xjfp_loginBtn setTitleColor:XJCOLOR_RGB(17, 17, 17) forState:UIControlStateNormal];
    [self.xjfp_loginBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];

    self.xjfp_loginBtn.titleLabel.font = XJFONTSET_R(15);
    self.xjfp_loginBtn.layer.masksToBounds = YES;
    self.xjfp_loginBtn.layer.cornerRadius = ADAPTHeight(40)/2;
    [self.xjfp_loginBtn addTarget:self action:@selector(xjfp_loginBtnClick) forControlEvents:UIControlEventTouchUpInside];
    self.xjfp_loginBtn.sd_layout
    .leftSpaceToView(self, ADAPTWidth(26))
    .rightSpaceToView(self, ADAPTWidth(26))
    .topSpaceToView(self.fpRepasswordTextField, ADAPTHeight(40))
    .heightIs(ADAPTHeight(40));
    
}

#pragma mark - 监听手机号
- (void)regist_phoneTextDidChange:(UITextField *)textField{
    if ([textField.text isPhoneNumber]) {
        XJLog(@"手机号 = %@",textField.text);
        self.xjCodeView.codeBtn.backgroundColor = MAIN_COLOR;
        self.xjCodeView.codeBtn.userInteractionEnabled = YES;
    }else{
        self.xjCodeView.codeBtn.backgroundColor = MAINS_COLOR;
        self.xjCodeView.codeBtn.userInteractionEnabled = NO;
    }
}

#pragma mark 获得验证码
- (void)getCheckCode:(NSString *)checkCode{
    
    XJLog(@"验证码 :%@",checkCode);
    XJLog(@"手机号是:%@",self.fpPhoneTextField.text);
    
    [XJHttpTool requestSendCodeWithPhone:self.fpPhoneTextField.text type:@"forget" successH:^(id responseObjc) {
        
        XJLog(@"%@",responseObjc);
        if ([responseObjc[@"code"] isEqualToNumber:@0]) {
            [JRToast showWithText:@"验证码已发送"];
        }else {
            [JRToast showWithText:@"验证码发送失败"];
        }
        
    } failureH:^(NSError *error) {
        [MBProgressHUD showError:error.description];
    }];
}

- (void)xjfp_loginBtnClick{
    
    XJLog(@"login");
    if ([self.delegate respondsToSelector:@selector(fpClickLoginHandler)]) {
        [self.delegate fpClickLoginHandler];
    }
    

}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
